<template>
	<el-dialog v-model="visible" title="联系人信息" :close-on-click-modal="false">
		<el-form ref="dataFormRef" label-width="220" :label-position="'top'" :model="dataForm" :rules="dataRules">
			<el-form-item label="与本人关系" prop="ctctRltTpCd">
				<fast-select v-model="dataForm.ctctRltTpCd" style="width: 100%" dict-type="mpi_ctct_rlt_tp_cd"
					placeholder="与本人关系" clearable filterable></fast-select>
			</el-form-item>
			<el-form-item label="联系人" prop="ctctRltNm">
				<el-input placeholder="联系人" maxlength="50" v-model="dataForm.ctctRltNm"></el-input>
			</el-form-item>
			<el-form-item label="联系方式" prop="ctctTelNo">
				<el-input placeholder="联系方式" v-model="dataForm.ctctTelNo"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" :loading="btnLoading" @click="subFormHandle">确定</el-button>
		</template>
	</el-dialog>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted, nextTick } from 'vue'

import { ElMessage } from 'element-plus'
import { savePatientPerson } from '@/api/patient-primary/patient-mana'
const visible = ref(false)
const btnLoading = ref(false)
const dataFormRef = ref()
const dataForm = reactive({
	ctctRltTpCd: '',
	ctctRltNm: '',
	ctctTelNo: '',
	empi: ''
})

const emit = defineEmits(['close'])

const dataRules = ref({
	ctctRltTpCd: [
		{
			required: true,
			message: '与本人关系不能为空',
			trigger: 'change'
		}
	],
	ctctRltNm: [
		{
			required: true,
			message: '联系人不能为空',
			trigger: 'blur'
		}
	],
	ctctTelNo: [
		{
			required: true,
			message: '联系方式不能为空',
			trigger: 'blur'
		}
	]
})

const init = (data, empi) => {
	btnLoading.value = false
	visible.value = true

	nextTick(() => {

		if (dataFormRef.value) {
			dataFormRef.value.resetFields()
		}
		dataForm.empi = empi

		if (data) {
			Object.assign(dataForm, data)
		} else {
			dataForm.id = ''
		}
	})

}

const subFormHandle = () => {
	dataFormRef.value.validate(valid => {
		if (!valid) {
			return false
		}
		btnLoading.value = true

		savePatientPerson(dataForm)
			.then(res => {
				ElMessage.success({
					message: '保存成功',
					duration: 500,
					onClose: () => {
						btnLoading.value = false
						visible.value = false
						emit('close')
					}
				})
			})
			.catch(() => {
				btnLoading.value = false
			})
	})
}

defineExpose({
	init
})
</script>
<style lang="less" scoped></style>
